package org.iteam.xlz.core.service.terminal;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.terminal.ProductTerminal;

public class TerminalProductService extends BaseService<ProductTerminal>{

	private static TerminalProductService instance;
	
	private TerminalProductService(){}
	
	public static TerminalProductService getInstance(){
		if(null==instance){
			instance=new TerminalProductService();
		}
		return instance;
	}
	
	public long counts(){
		try {
			return super.counts("select count(1) from productterminal");
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<ProductTerminal> query(long page,long rows){
		try {
			return super.query("select l.ptid,l.status,r.tid,p.pid,r.serialno,r.community,p.productname,"+
			"l.productamount from productterminal l left join terminal r on l.tid=r.tid"+
			" left join product p on l.pid=p.pid"+
			" where 1=1 limit ?,?",(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<ProductTerminal>();
		}
	}
	
	public boolean create(String pid,String tid,String productamount,String creator){
		Connection con=null;
		try {
			con=super.getConnection(false);
			SqlBuilder sqlA=SqlBuilder.createInsert("productterminal");
			sqlA.addField("ptid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sqlA.addField("pid",pid);
			sqlA.addField("tid",tid);
			sqlA.addField("productamount",productamount);
			sqlA.addField("status",0);
			SqlBuilder sqlB=SqlBuilder.createInsert("LOGPRODUCTTERMINAL");
			sqlB.addField("logid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sqlB.addField("pid",pid);
			sqlB.addField("tid",tid);
			sqlB.addField("logtype",0);
			sqlB.addField("productamount",productamount);
			sqlB.addField("createtime",DateUtil.currentTime());
			sqlB.addField("creator",creator);
			int i=super.updates(con,sqlA.getSql(),sqlA.getParams());
			int j=super.updates(con,sqlB.getSql(),sqlB.getParams());
			if(i==1&&i==j){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	
	}
	
	public boolean update(ProductTerminal model,int amount,String creator){
		Connection con=null;
		try {
			con=super.getConnection(false);
			SqlBuilder sqlB=SqlBuilder.createInsert("LOGPRODUCTTERMINAL");
			sqlB.addField("logid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sqlB.addField("pid",model.getPid());
			sqlB.addField("tid",model.getTid());
			sqlB.addField("logtype",2);
			sqlB.addField("productamount",model.getProductamount()+amount);
			sqlB.addField("createtime",DateUtil.currentTime());
			sqlB.addField("creator",creator);
			int i=super.updates(con,"update productterminal set productamount=productamount+? where ptid=?",amount,model.getPtid());
			int j=super.updates(con,sqlB.getSql(),sqlB.getParams());
			if(i==1&&i==j){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	
	}
	
	public boolean down(String ptid,String creator){
		Connection con=null;
		try {
			con=super.getConnection(false);
			ProductTerminal model=super.show(con,"select * from productterminal where ptid=?",ptid);
			if(null==model){
				return false;
			}
			int i=super.updates(con,"update productterminal set status=1 where 1=1 and ptid=? and status=0",ptid);
			SqlBuilder sqlB=SqlBuilder.createInsert("LOGPRODUCTTERMINAL");
			sqlB.addField("logid",UUID.randomUUID().toString().replace("-","").toUpperCase());
			sqlB.addField("pid",model.getPid());
			sqlB.addField("tid",model.getTid());
			sqlB.addField("logtype",1);
			sqlB.addField("createtime",DateUtil.currentTime());
			sqlB.addField("creator",creator);	
			int j=super.updates(con, sqlB.getSql(),sqlB.getParams());
			if(i==1&&i==j){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public ProductTerminal findOne(String ptid){
		try {
			return super.show("select l.ptid,r.serialno,l.tid,l.pid,l.productamount "
			+ "from productterminal l left join terminal r on l.tid=r.tid where 1=1 and l.ptid=?",ptid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ProductTerminal();
		}
	}
}
